﻿using System;
using System.IO;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;

using System.Drawing;
using System.Collections.Generic;
namespace HuoTu.Common
{
    /// <summary>
    /// 文件、目录操作
    /// </summary>
    public class SysFile
    {
        #region 文件夹操作
        /// <summary>
        /// 根据路径建立文件夹
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        public static void CreateFolder(string folderPath)
        {
            string path = SysString.ChangePath(folderPath);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }
        /// <summary>
        /// 检测文件夹路径是否存在
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        /// <returns></returns>
        public static bool CheckFolder(string folderPath)
        {
            folderPath = SysString.ChangePath(folderPath);
            if (Directory.Exists(folderPath))
                return true;
            else
                return false;
        }

        /// <summary>
        /// 读取文件夹下的文件
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        /// <returns></returns>
        public static string[] GetFolders(string folderPath)
        {
            folderPath = SysString.ChangePath(folderPath);
            if (!CheckFolder(folderPath))
                return null;
            return Directory.GetDirectories(folderPath);
        }

        /// <summary>
        /// 移动文件夹
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        /// <param name="destFolderPath">目标文件夹路径</param>
        public static void MoveFolder(string folderPath, string destFolderPath)
        {
            folderPath = SysString.ChangePath(folderPath);
            DirectoryInfo f = new DirectoryInfo(folderPath);
            if (f.Exists)
            {
                f.MoveTo(destFolderPath);
            }

        }

        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        public static void DeleteFolder(string folderPath)
        {
            folderPath = SysString.ChangePath(folderPath);
            DirectoryInfo f = new DirectoryInfo(folderPath);
            if (f.Exists)
            {
                f.Delete();
            }
        }
        #endregion

        #region 文件操作
        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="filePath"></param>
        public static void CreateFile(string filePath)
        {
            string file = SysString.ChangePath(filePath);
            if (!File.Exists(file))
            {
                File.Create(file);
            }
        }

        /// <summary>
        /// 检测文件是否存在
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static bool CheckFile(string filePath)
        {
            string file = SysString.ChangePath(filePath);
            if (File.Exists(file))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 读取文件夹下的文件
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        /// <returns></returns>
        public static string[] GetFiles(string folderPath)
        {
            folderPath = SysString.ChangePath(folderPath);
            if (!CheckFolder(folderPath))
                return null;
            return Directory.GetFiles(folderPath);
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static string ReadFile(string filePath)
        {
            filePath = SysString.ChangePath(filePath);
            FileStream fs = null;
            StreamReader r = null;
            string str = string.Empty;
            try
            {
                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                r = new StreamReader(fs, Encoding.UTF8);
                str = r.ReadToEnd();
                r.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                if (r != null) r.Close();
                if (fs != null) fs.Close();
            }
            return str;
        }

        /// <summary>
        /// 按行读取数据
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static List<string> ReadFileLine(string filePath)
        {
            try
            {
                List<string> list = new List<string>();
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {

                    using (StreamReader r = new StreamReader(fs, Encoding.UTF8))
                    {
                        while (r.Peek() >= 0)
                        {
                            list.Add(r.ReadLine());
                        }
                    }
                }

                return list;
            }
            catch (Exception ex) { }

            return null;
        }


        /// <summary>
        /// 写入文件数据
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="text">内容</param>
        public static void WriteFile(string filePath, string text)
        {
            filePath = SysString.ChangePath(filePath);
            TextWriter writer = null;
            FileStream fs = null;
            try
            {
                if (File.Exists(filePath)) File.Delete(filePath);
               
                fs = new FileStream(filePath, FileMode.CreateNew);
                writer = new StreamWriter(fs, Encoding.UTF8);
                writer.Write(text);
                writer.Close();
                fs.Close();
                return;
            }
            catch (Exception ex)
            {
                if (writer != null) writer.Close();
                if (fs != null) fs.Close();
            }
        }

        /// <summary>
        /// 追加一行文件数据
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="text">内容</param>
        public static void WriteFileLine(string filePath, string text)
        {
            filePath = SysString.ChangePath(filePath);
            try
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Append))
                {
                    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                    {
                        w.WriteLine(text);
                    }
                }

                return;
            }
            catch (Exception ex) {
                throw ex;
            }
        }

        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="destFilePath">目标文件路径</param>
        public static void MoveFile(string filePath, string destFilePath)
        {
            try
            {
                filePath = SysString.ChangePath(filePath);
                FileInfo f = new FileInfo(filePath);
                if (f.Exists)
                {
                    f.MoveTo(destFilePath);
                }
            }
            catch(Exception ex){
                throw ex;
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filePath">文件路径</param>
        public static void DeleteFile(string filePath)
        {
            try
            {
                filePath = SysString.ChangePath(filePath);
                FileInfo f = new FileInfo(filePath);
                if (f.Exists)
                {
                    f.Delete();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 分块下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="outFileName">输出显示文件名</param>
        public static void TransferFileByBlock(string url, string outFileName)
        {
            FileStream iStream = null;
            try
            {
                //HttpContext.Current.Response.Clear();
                HttpContext.Current.Server.ScriptTimeout = 999999;
                FileInfo fileInfo = new FileInfo(url);               
                if (fileInfo.Exists)
                {
                    string ext = fileInfo.Extension;
                    const long ChunkSize = 102400;//100K 每次读取文件，只读取100K，这样可以缓解服务器的压力 
                    byte[] buffer = new byte[ChunkSize];

                    HttpContext.Current.Response.Clear();

                    iStream = File.OpenRead(url);
                    long dataLengthToRead = iStream.Length;//获取下载的文件总大小 
                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(outFileName + fileInfo.Extension));
                    HttpContext.Current.Response.AddHeader("Content-Length", dataLengthToRead.ToString());
                    HttpContext.Current.Response.Charset = "UTF-8";
                    while (dataLengthToRead > 0 && HttpContext.Current.Response.IsClientConnected)
                    {
                        int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 
                        HttpContext.Current.Response.OutputStream.Write(buffer, 0, lengthRead);
                        HttpContext.Current.Response.Flush();
                        dataLengthToRead = dataLengthToRead - lengthRead;
                    }
                    //HttpContext.Current.Response.Flush();
                    //HttpContext.Current.Response.Close();
                    iStream.Close();
                }
            }
            catch (Exception ex) { }
            finally
            {
                if (iStream != null) iStream.Close();
            }
        }
        #endregion

        #region 图片操作
        /// <summary>
        /// 获取图片宽度
        /// </summary>
        /// <param name="filePath">图片相对路径</param>
        /// <returns></returns>
        public static int GetPicWidth(string filePath)
        {
            int width = 0;
            filePath = SysString.ChangePath(filePath);
            if (File.Exists(filePath))
            {
                Image image = Image.FromFile(filePath);
                width = image.Width;
                image.Dispose();
            }
            return width;
        }
        /// <summary>
        /// 获取图片高度
        /// </summary>
        /// <param name="filePath">图片相对路径</param>
        /// <returns></returns>
        public static int GetPicHeight(string filePath)
        {
            int height = 0;
            filePath = SysString.ChangePath(filePath);
            if (File.Exists(filePath))
            {
                System.Drawing.Image image = System.Drawing.Image.FromFile(filePath);
                height = image.Height;
                image.Dispose();
            }
            return height;
        }
        #endregion
    }
}
